//
//  MallListenViewController.m
//  LiteraryTune
//
//  Created by 神州四达 on 2018/1/18.
//  Copyright © 2018年 cxd. All rights reserved.
//  听书

#import "MallListenViewController.h"
#import "BookCollectCell.h"
#import "FooterCollectionReusableView.h"
#import "ClassifyCircleCollectionViewCell.h"
#import "CateCenterCollectionReusableView.h"
#import "BookTypeSearchView.h"
//#import "BookListModel.h"
#import "AudioClassifyModel.h"
#import "ClassifyHeaderView.h"
#import "VideoDetailVC.h"
#import "AudioClassifyListVC.h"

@interface MallListenViewController () <UICollectionViewDelegate,UICollectionViewDataSource,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource, UICollectionViewDelegateFlowLayout, ClassifyHeaderViewDelegate>

@property (nonatomic, strong)UICollectionView *showCollectionView;
//数据源
@property (nonatomic, strong)NSArray<AudioClassifyModel *> *modelArray;

@end

@implementation MallListenViewController
{
    BOOL requestCompleted;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    requestCompleted = NO;
    [self getNetData];
    [self buildUI];
}

- (void)getNetData {
    
    [CXDNetHelp getRequestWithURLString:@"http://www.wenqujingdian.com/index.php/jiekou/getAudioClass" parameters:nil success:^(id  _Nullable responseObject) {
        requestCompleted = YES;
        [self.showCollectionView reloadEmptyDataSet];
        self.modelArray = [AudioClassifyModel mj_objectArrayWithKeyValuesArray:responseObject];
        [self.showCollectionView reloadData];
        
    } failure:^(NSError * _Nullable error) {
        requestCompleted = YES;
        [self.showCollectionView reloadEmptyDataSet];
    }];
}

- (void)buildUI {
    [self.view addSubview:self.showCollectionView];
    self.showCollectionView.emptyDataSetDelegate = self;
    self.showCollectionView.emptyDataSetSource = self;
}

#pragma mark - 懒加载
- (UICollectionView *)showCollectionView {
    if (!_showCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kScreenWidth ) / 3, 150);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _showCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44 - UIApplication.sharedApplication.statusBarFrame.size.height) collectionViewLayout:layout];
        _showCollectionView.delegate = self;
        _showCollectionView.dataSource = self;
        _showCollectionView.backgroundColor = RGB(238, 238, 238);
        _showCollectionView.showsVerticalScrollIndicator = NO;
        [_showCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([BookCollectCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class])];
        [_showCollectionView registerClass:ClassifyHeaderView.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    }
    return _showCollectionView;
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return self.modelArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.modelArray[section].childs.count > 3? 3: self.modelArray[section].childs.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BookCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class]) forIndexPath:indexPath];
    cell.backView.hidden = YES;
    AudioClassifyItemModel *tempModel = self.modelArray[indexPath.section].childs[indexPath.item];
    [cell.bookImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",RootHomeURL,tempModel.spic]]];
    cell.nameLabel.text = tempModel.yinpinname;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    VideoDetailVC *detailVC = [[VideoDetailVC alloc] init];
    AudioClassifyItemModel *tempModel = self.modelArray[indexPath.section].childs[indexPath.item];
    detailVC.navigationItem.title = tempModel.yinpinname;
    detailVC.videoId = tempModel.yinpinId;
    [self.navigationController pushViewController:detailVC animated:YES];
}

//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
//        FooterCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([FooterCollectionReusableView class]) forIndexPath:indexPath];
//        return view;
//    }
//    return nil;
//}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        ClassifyHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.classifyTitle = self.modelArray[indexPath.section].name;
        header.delegate = self;
        header.section = indexPath.section;
        return header;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(kScreenWidth, 45);
}

- (void)classifyHeaderView:(ClassifyHeaderView *)headerView moreBtnDidClick:(UIButton *)button atSection:(NSInteger)section {
    
    AudioClassifyListVC *vc = [[AudioClassifyListVC alloc] init];
    vc.mediaId = self.modelArray[section].id;
    vc.mediaName = self.modelArray[section].name;
    [self.navigationController pushViewController:vc animated:YES];
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    
    return requestCompleted && self.modelArray.count == 0;
}

@end
